package com.wuxiaofan.product.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuxiaofan.product.entity.Brand;
import com.wuxiaofan.product.entity.custom.BrandCustom;
import com.wuxiaofan.product.service.BrandService;
import com.wuxiaofan.common.vo.ReturnResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.swagger.annotations.Api;

@Slf4j
@RestController
@Api("品牌")
@RequestMapping("/brand")
public class BrandApi {


    @Autowired
    private BrandService pmsBrandService;

    @GetMapping("/findById/{id}")
    public ReturnResult findById(@PathVariable("id") String id){
        Brand pmsBrand = pmsBrandService.findById(id);
        return ReturnResult.success("",pmsBrand);
    }

    @RequestMapping("findByMap")
    public ReturnResult findByMap(@RequestBody JSONObject jsonObject){
        Map paraMap = new HashMap();
        paraMap.putAll(jsonObject);
        List<BrandCustom> pmsBrandCustoms = pmsBrandService.findByMap(paraMap);
        return ReturnResult.success("",pmsBrandCustoms);
    }

    @RequestMapping("all")
    public ReturnResult all(){
        Map paraMap = new HashMap();
        List<BrandCustom> pmsBrandCustoms = pmsBrandService.findByMap(paraMap);
        return ReturnResult.success("",pmsBrandCustoms);
    }

    @RequestMapping("findByPage")
    public ReturnResult findByPage(@RequestBody JSONObject jsonObject){
        Page page = new Page(jsonObject.getIntValue("current"),jsonObject.getIntValue("size"));
        Map paraMap = new HashMap();
        paraMap.putAll(jsonObject);
        IPage<BrandCustom> pmsBrandCustoms = pmsBrandService.findByMap(paraMap,page);
        return ReturnResult.success("",pmsBrandCustoms);
    }

    @RequestMapping("save")
    public ReturnResult save(@RequestBody Brand pmsBrand){
        pmsBrand = pmsBrandService.save(pmsBrand);
        return ReturnResult.success("保存成功",pmsBrand);
    }

    @RequestMapping("update")
    public ReturnResult update(@RequestBody JSONObject jsonObject){
        Map paraMap = jsonObject;
        pmsBrandService.updateMapById(paraMap);
        return ReturnResult.success("更新成功");
    }


    @RequestMapping("delByIds")
    public ReturnResult delByIds(@RequestBody JSONArray jsonArray){
        log.info("删除品牌id信息 jsonArray:{}",jsonArray);
        List<Long> ids = jsonArray.toJavaList(Long.class);
        pmsBrandService.delByIds(ids);
        return ReturnResult.success("删除成功");
    }


}
